package com.oauth2.repository;

import java.util.Date;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.oauth2.entity.OauthAccessToken;

public interface OauthAccessTokenRepository extends JpaRepository<OauthAccessToken, String>{
	
	/**
	 * 根据clientid和username查询oauth_access_token
	 * @param clientId
	 * @param username
	 * @return
	 */
	@Query(value = "select * from  oauth_access_token where client_id=:clientId and user_name=:username ", nativeQuery = true)  
	public OauthAccessToken findByClientAndUsername(@Param("clientId")String clientId,@Param("username")String username);
	
	/**
	 * 根据clientid和username更新refresh_token
	 * @param clientId
	 * @param username
	 * @param createTime
	 * @param refresh_token
	 * @return
	 */
	@Modifying
	@Query(value="update oauth_access_token  set refresh_token=:refresh_token,create_time=:createTime where client_id=:clientId and user_name=:username", nativeQuery = true)
	public int updateRefreshToken(@Param("clientId")String clientId,@Param("username")String username,@Param("createTime")Date
			createTime,@Param("refresh_token")String
			refresh_token);
	
	/**
	 * 根据refresh_token查询
	 * @param refreshToken
	 * @return
	 */
	@Query(value = "select * from  oauth_access_token where refresh_token=:refreshToken ", nativeQuery = true)  
	public OauthAccessToken findByRefreshToken(@Param("refreshToken")String refreshToken);
	
}
